Ext.ns('journal.home');

// {{{
journal.home.Menubar = Ext.extend(Ext.Toolbar, (function() {

    var preferenceDialog = null;

    return {
        // configurables
        // anything what is here can be configured from outside

        /**
         * Invalidate session and return to login screen.
         */
        logoutHandler : function() {
            window.location = 'login?logout';
        },

        /**
         * Called by journal.home.Main when a key is pressed while the menu is shown.
         * 
         * @param e - event object
         * @param key - converted to uppercase. Comparable to e.<letter>
         */
        handlerKeypress : function(e, key) {

            this.e = e;
            this.key = key;

            this.items.each(function(button) {
                if (button.hasVisibleMenu()) {
                    button.menu.items.each(function(submenu) {
                        if (submenu.ctype == 'Ext.menu.Item') {//skip separators
                            var underlinedText = submenu.text.substr(submenu.text.indexOf('<u>') + 3, 1);
                            if (String.fromCharCode(this.key).toLowerCase() == underlinedText.toLowerCase()) {
                                submenu.fireEvent('click');
                                button.hideMenu();
                                return false;
                            }
                        }
                    }.createDelegate(this));
                    return false;
                }
            }.createDelegate(this));

            /*
             * switch (key) { case e.N : break; case e.I : break; case e.X : break; case e.O : this.logoutHandler();
             * break; default : }
             */
        },

        // {{{
        initComponent : function() {
            // hard coded config - cannot be changed from outside
            var config = {
                height : 27,
                width : '100%',
                items : [
                        {
                            xtype : 'tbbutton',
                            id : journal.Constant.Menu.FILE,
                            text : 'File',
                            menu : {
                                items : [
                                        {
                                            id : journal.Constant.Menu.NEW,
                                            text : '<u>N</u>ew...',
                                            iconCls : 'icon-new',
                                            handler : function(btn, event) {
                                                if (!this.menu.get(journal.Constant.Button.NEW_ENTRY).hidden) {
                                                    this.newEntryHandler();
                                                } else {
                                                    this.newBranchHandler();
                                                }
                                            },
                                            newEntryHandler : function() {
                                                var targetNode = Ext.getCmp(journal.Constant.Id.ENTRY_TREE)
                                                        .getSelectionModel().getSelectedNode();
                                                journal.home.Main.editorPanel.newTabHandler(targetNode);
                                            },
                                            newBranchHandler : function() {
                                                var tree = Ext.getCmp(journal.Constant.Id.ENTRY_TREE);
                                                var targetNode = tree.getSelectionModel().getSelectedNode();
                                                if (targetNode == null) {
                                                    targetNode = tree.root;
                                                } else if (targetNode.isLeaf()) {
                                                    targetNode = tree.root;
                                                }
                                                journal.home.EntryExplorerHandler.addFolderHandler(targetNode);
                                            },
                                            menu : {
                                                items : [ {
                                                    id : journal.Constant.Menu.NEW_ENTRY,
                                                    hidden : true,
                                                    text : 'New Entry',
                                                    iconCls : 'icon-addItem',
                                                    handler : function(a, event) {
                                                        this.parentMenu.ownerCt.newEntryHandler();
                                                    }
                                                }, {
                                                    id : journal.Constant.Menu.NEW_BRANCH,
                                                    text : 'New Branch',
                                                    iconCls : 'icon-addBranch',
                                                    handler : function(a, event) {
                                                        this.parentMenu.ownerCt.newBranchHandler();
                                                    }
                                                } ]
                                            }

                                        }, {
                                            id : journal.Constant.Menu.SAVE,
                                            text : 'Save',
                                            iconCls : 'icon-save',
                                            disabled : true,
                                            handler : function(close) {
                                                journal.home.Main.editorPanel.saveTab(false);
                                            }
                                        }, '-', {
                                            text : '<u>I</u>mport...',
                                            iconCls : 'icon-import',
                                            handler : function() {
                                                var win = new journal.home.ImportWindow();
                                                win.on('render', function(w) {
                                                    w.height = 150;
                                                    w.width = 500;
                                                });
                                                win.show();
                                            }
                                        }, {
                                            text : 'E<u>x</u>port...',
                                            iconCls : 'icon-export',
                                            handler : function() {
                                                window.open('home?action=exportCodeXml', '_self');
                                                return true;
                                            }
                                        }, new Ext.menu.Separator({
                                            id : 'logout-separator'
                                        }), {
                                            text : 'Log<u>o</u>ut',
                                            iconCls : 'icon-logout',
                                            handler : function() {
                                                this.logoutHandler();
                                            }.createDelegate(this)
                                        } ]
                            }
                        },
                        {
                            xtype : 'tbbutton',
                            id : 'btnWindow',
                            text : 'Window',
                            menu : {
                                items : [ {
                                    text : 'Show View',
                                    disabled : true
                                }, '-', {
                                    text : 'Preferences...',
                                    handler : function() {
                                        if (preferenceDialog == null) {
                                            preferenceDialog = new journal.home.PreferenceDialog({
                                                user : this.user
                                            });
                                            preferenceDialog.on('render', function(w) {
                                                w.height = 315;
                                                w.width = 700;
                                            });
                                        }
                                        preferenceDialog.show();
                                    }.createDelegate(this)
                                } ]
                            }
                        },
                        {
                            xtype : 'tbfill'
                        },
                        this.user,
                        '-',
                        {
                            xtype : 'tbbutton',
                            text : '',
                            iconCls : 'icon-phflag',
                            handler : function() {
                                Ext.MessageBox.show({
                                    title : 'Journal Application',
                                    msg : 'Made in the Philippines<br><a href="' + journal.Constant.PROJECT_HOME
                                            + '" target="_blank">Project Home</a>',
                                    buttons : Ext.MessageBox.OK,
                                    icon : Ext.MessageBox.INFO

                                });
                            }
                        }, '-', {
                            xtype : 'tbbutton',
                            name : 'btnLogout',
                            text : 'Logout',
                            iconCls : 'icon-logout',
                            handler : function() {
                                this.logoutHandler();
                            }.createDelegate(this)
                        } ]
            };// var config

            // apply config
            Ext.apply(this, Ext.apply(this.initialConfig, config));

            // call parent
            journal.home.Menubar.superclass.initComponent.apply(this, arguments);

        } // e/o function initComponent

    // }}}
    };
})()

); // e/o extend

// register xtype
Ext.reg('journal.home.Menubar', journal.home.Menubar);
// }}}
